layui.use('element', function () {
    var $ = layui.jquery;
    var element = layui.element();
    //初始化日期时间
    refreshCalendarClock();
    //加载天气
    initWeather();
    //每两小时加载一次天气
    window.setInterval("initWeather()",  2 * 60 * 60 * 1000);
    reflash();
    function reflash() {
        var ul = $("#ul");
        var adminBody = $("#admin-body");
        var content = $("#content");
        var h = ul.css("height").split("px")[0];
        var H = adminBody.css("height").split("px")[0];
        var hh = H - h - 1 + "px";
        content.css({"height": hh})
    }

    $(window).resize(function () {
        reflash()
    });
    $(".click_to_action").eq(2).trigger("click");
    //等待dom元素加载完毕.
    $(".left .level1>a").click(function () {
        var len = $(this).next("ul").length;
        if (len == 1) {
            var flag = $(this).find("i").is(".down");
            if (flag) {
                $(this).parent().children('a').removeClass('current')
                    .find('i').removeClass('down').parent().next().slideUp('slow', 'easeOutQuad');
            } else {
                $(this).addClass('current') //给当前元素添加"current"样式
                    .find('i').addClass('down') //小箭头向下样式
                    .parent().next().slideDown('slow', 'easeOutQuad') //下一个元素显示
                    .parent().siblings().children('a').removeClass('current') //父元素的兄弟元素的子元素去除"current"样式
                    .find('i').removeClass('down').parent().next().slideUp('slow', 'easeOutQuad'); //隐藏
                return false; //阻止默认时间
            }
        } else {
            $(this).parent().siblings().children('a').removeClass('current')
                .find('i').removeClass('down').parent().next().slideUp('slow', 'easeOutQuad');
        }
    });
    //系统的退出
    var admin = $(".do-admin");
    admin.click(function () {
        layui.use('layer', function () {
            layer.alert('请再次确认是否要退出系统！', {
                title: "确定退出系统？",
                icon: 3,
                skin: 'layui-layer-molv', //样式类名
                closeBtn: 1,
                btn: ['确定', '取消']
            }, function () {
                location.href = basePath + "logout.jsp"
            });
        })
    })
});

function add(title2, obj, url) {
    var title2 = title2;
    var url = url;
    var obj = obj;
    layui.use('element', function () {
        var $ = layui.jquery;
        var element = layui.element();
        var tabs = $(".layui-tab-title").children("li");
        var str2 = obj.find("cite").html();
        var k = 0;
        $.each(tabs, function (i) {
            var str = tabs.eq(i).find("cite").html();
            if (str == str2) {
                k = 1;
                element.tabChange('demo', i + 1);
                $('iframe').eq(i).attr('src', url);
            }
        })
        if (k == 0) {
            var content = '<iframe  frameborder="0" scrolling="no"  src="' + url + '" style="width:100%;height:100%;margin:0;padding:0;position:absolute;top:0;bottom:0;"></iframe>';
            var index = tabs.length + 1;
            element.tabAdd('demo', {
                title: title2,
                content: content,
                id: index
            });
            element.tabChange('demo', index);
        }
        var tabs = $(".layui-tab-title").children("li");
        $.each(tabs, function (i) {
            tabs.eq(i).attr("lay-id", i + 1)
        })
        element.tabChange('demo', index);
    });
    $.each($(".click_to_action"), function (i) {
        var cite = $(".click_to_action").eq(i).children("cite");
        var em = $(".click_to_action").eq(i).children("em");
        $(".click_to_action").eq(i).removeClass('bg');
        cite.css("color", "");
        em.css("color", "");
    })
    var cite = obj.children("cite");
    var em = obj.children("em");
    em.css("color", "#fff");
    cite.css("color", "#fff");
    obj.addClass("bg");
}
//加载天气
function initWeather() {
    $("#weather").leoweather({format: '<span style="margin-right:10px;">{城市}</span>' +
    '<span style="margin-right:10px;"><img src="' + basePath + 'images/weathericon/{图标}.png" title="{天气}"/></span>' +
    '<span style="margin-right:10px;" title="{风级}级">{风向}</span><span>{夜间气温}℃ ~ {白天气温}℃</span>'});
}